﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XiDainChangAn.OA.IBLL;
using XiDainChangAn.OA.WebApp.Models;
using XiDianChangAn.OA.Model;

namespace XiDainChangAn.OA.WebApp.Controllers
{

    [MyLoginCheckFilterAttribute(IsCheck = false)]
    public class ActionInfoController : Controller
    {
        public IActionInfoService ActionInfoService { get; set; }
        // GET: Action
        public ActionResult Index()
        {
            return View();
        }

        public JsonResult ShowAllAction()
        {
            int pageInde = Request["page"] == null ? 1 : int.Parse(Request["page"]);
            int pageSize = Request["rows"] == null ? 1 : int.Parse(Request["rows"]);
            int totalCount;
            var delFalg = (short)XiDianChangAn.OA.Model.Enum.DelFlagEnum.Normal;
            var userList = ActionInfoService.GetPageEntitys<long>(pageSize, pageInde, out totalCount, true, u => u.DelFlag == delFalg, u => u.Id).ToList();
            var temp = from u in userList
                       select new
                       {
                           Id = u.Id,
                           ActionName = u.ActionName,
                           IsMenu=u.IsMenu,
                           Remark=u.Remark,
                           Sort = u.Sort
                       };
            //客户端就是遍历rows  jq easy-ui就是遍历rows
            return Json(new { rows = temp, total = totalCount }, JsonRequestBehavior.AllowGet);
        }

        #region 02，添加权限信息+JsonResult AddAction(ActionInfo model)
        /// <summary>
        /// 添加权限信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public JsonResult AddAction(ActionInfo model)
        {
            if (ModelState.IsValid)
            {
                model.DelFlag = (short)XiDianChangAn.OA.Model.Enum.DelFlagEnum.Normal;
                model.CreateTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                model.ModfiedOn = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                ActionInfoService.Add(model);
            }
            return Json("ok");

            //string si = new string();

        } 
        #endregion

        #region 03，权限编辑
        /// <summary>
        /// 获取要编辑的权限的信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult EditAction(int id)
        {
            ActionInfo model = ActionInfoService.GetEntitys(u => u.Id == id).FirstOrDefault();
            return Json(model, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 编辑权限信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditAction(ActionInfo model)
        {
            if (ModelState.IsValid)
            {
                model.ModfiedOn = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                ActionInfoService.UpDate(model);
            }
            
            return Content("ok");
        } 
        #endregion

        #region 04，删除权限信息+ActionResult Delete(string strId)
        /// <summary>
        /// 删除权限信息
        /// </summary>
        /// <param name="strId"></param>
        /// <returns></returns>
        public ActionResult Delete(string strId)
        {
            string[] str = strId.Split(new char[] { ',' });
            foreach (string item in str)
            {

                ActionInfoService.Delete(ActionInfoService.GetById(int.Parse(item)));
            }
            return Content("ok");
        }  
        #endregion 

    }
}